package com.miyu.client1.controller.bookController;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.service.book.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RequestMapping("/book")
@RestController
public class bookController {


    @Autowired
    private bookService bs;
    @RequestMapping("/search")
    public JSONObject search(@RequestBody JSONObject rq){
        JSONObject ts = new JSONObject();
      //  rq.put("search","仙");
        JSONArray rs = bs.searchBook(rq);
       // JSONObject rs = new JSONObject();
        System.out.println(rs);
        ts.put("data",rs);
        ts.put("code","0");
        ts.put("count",rs.size());
        ts.put("msg","接收数据成功");

        return ts;
    }

    @RequestMapping("/queryBookList")
    public JSONObject queryBookList(@RequestBody JSONObject rq) throws Exception {
        JSONObject ts = new JSONObject();
        JSONObject rs =  bs.queryBookList(rq.getString("bookCode"), "ddxs");

        ts.put("data",rs);
        ts.put("code","0");
        ts.put("count",rs.getJSONArray("zjList").size());
        ts.put("msg","接收数据成功");

        return ts;
    }
    @RequestMapping("readBook/queryText")
    public JSONObject queryText(@RequestBody JSONObject rq){
    /*
        ModelAndView mav = new ModelAndView();
        System.out.println(rq);
       String source = request.getRequestURI();
        source = source.replace("/book/readBook/","");
     //   source = source.substring(source.indexOf("readBook")+8,source.length());
        String bookCode = source.split("/")[0];
        String chapterCod = source.split("/")[1];
        JSONObject rs = bs.readText("ddxs", bookCode, chapterCod);
        JSONObject ts = new JSONObject();
        ModelAndView mav = new ModelAndView();

        ts.put("data",rs);
        ts.put("code","0");
       // ts.put("count",rs.size());
        ts.put("msg","接收数据成功");
        mav.setViewName("/book/pageBook/readBook");
        mav.addObject("data",rs);*/
        String bookCode =  rq.getString("bookCode");
        String chapterCod = rq.getString("chapterCod");
        JSONObject rs = bs.readText("ddxs", bookCode, chapterCod);
        JSONObject ts = new JSONObject();
        ts.put("data",rs);
        ts.put("code","0");
        ts.put("msg","接收数据成功");
        return  ts;
    }
    @RequestMapping("downloadBook")
    public HttpServletResponse  downloadBook(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String bookCode = request.getParameter("bookCode");
        String bookName= request.getParameter("bookName");
       String path = bs.downloadBook(bookCode,bookName);
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));

            response.reset();
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/octet-stream");
            int count =0;
            byte[] by = new byte[1024];
            //通过response对象获取OutputStream流
            OutputStream out=  response.getOutputStream();

            while((count=fis.read(by))!=-1){
                out.write(by, 0, count);//将缓冲区的数据输出到浏览器
            }
            fis.close();
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/queryBookRuleList")
    public JSONArray queryBookRuleList(){
        JSONArray rs = bs.queryBookRuleList();

        JSONObject ts = new JSONObject();

//        ts.put("data", rs);
//        ts.put("code","0");
//        ts.put("count",rs.size());
//        ts.put("msg","数据返回成功");

        return rs;
    }


    @RequestMapping("/saveOrupdate")
    public JSONObject saveOrupdate(@RequestBody JSONObject param){

        JSONObject rs =   bs.saveOrupdate(param);

        return rs;
    }

}
